package struts.form;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class Struts_xml {
	public static Map<String, XmlBean> struts_xml(String xmlPath)throws Exception{
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(xmlPath));
		Element root = document.getRootElement();
		
		Element actionroot = root.getChild("action-mapping");
		List<Element> actio = actionroot.getChildren();
		Map<String,XmlBean> xMap = new HashMap<String,XmlBean>();
		for(Element e : actio){
			XmlBean xBean = new XmlBean();
			xBean.setBeanName(e.getAttributeValue("name"));
			xBean.setPath(e.getAttributeValue("path"));
			xBean.setActionType(e.getAttributeValue("type"));
			
			Element actionform = root.getChild("formbeans");
			List<Element> form = actionform.getChildren();
			for(Element fe : form){
				if(fe.getAttributeValue("name").equals(e.getAttributeValue("name"))){
					xBean.setActionClass(fe.getAttributeValue("class"));
					break;
				}
			}
			List<Element> forward = e.getChildren();
			Map<String,String> map = new HashMap<String,String>();
			for(Element x : forward){
				map.put(x.getAttributeValue("name"), x.getAttributeValue("value"));
			}
			xBean.setActionForward(map);
			xMap.put(e.getAttributeValue("path"), xBean);
		}
		return xMap;
		
	}
}
